#include <bits/stdc++.h>
# define int long long
# define fir first
# define sec second
# define pb push_back
using namespace std;
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
int posi[cnst*4];
int arr[cnst];
int build(int a, int b, int id) {
if(a == b) return posi[id] = arr[a];
int mid = (a+b)/2;
return posi[id] = build(a, mid, id*2)+build(mid+1, b, id*2+1);
}
void update(int a, int b, int id, int pos, int val) {
if(a == pos && b == pos) {
posi[id] = val;
return;
}
int mid =(a+b)/2;
if(pos <= mid) update(a, mid, id*2, pos, val);
else update(mid+1, b, id*2+1, pos, val);
posi[id] = posi[id*2]+posi[id*2+1];
}
int get(int a, int b, int id, int from, int to) {
if(to < a || from > b) return 0;
if(from <= a && b <= to) return posi[id];
int mid = (a+b)/2;
return get(a, mid, id*2, from, to)+get(mid+1, b, id*2+1, from, to);
}
void solve() {
int n; cin >> n;
string s; cin >> s;
int fre[30];
int ans = 0;
vector<int> pos[30];
memset(fre, 0, sizeof(fre));
for(int i = 0; i<n; i++) {
int x = s[i]-'a'+1;
pos[x].pb(i+1);
update(1, n, 1, i+1, 1);
}
for(int i = 1; i<=n; i++) {
int x = s[(n-i)]-'a'+1;
ans += get(1, n, 1, 1, pos[x][fre[x]]-1);
update(1, n, 1, pos[x][fre[x]], 0);
fre[x]++;
// cerr << x << " " << pos[x][fre[x]] << endl;
// cerr << i << " " << ans << endl;
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
//2
1536C - Diluc and Kaeya | 1428C - ABBB |
1557A - Ezzat and Two Subsequences | 255A - Greg's Workout |
1059A - Cashier | 1389C - Good String |
1561A - Simply Strange Sort | 1337B - Kana and Dragon Quest game |
137C - History | 1443C - The Delivery Dilemma |
6C - Alice Bob and Chocolate | 1077C - Good Array |
285B - Find Marble | 6A - Triangle |
1729A - Two Elevators | 1729B - Decode String |
1729C - Jumping on Tiles | 1729E - Guess the Cycle Size |
553B - Kyoya and Permutation | 1729D - Friends and the Restaurant |
1606C - Banknotes | 580C - Kefa and Park |
342A - Xenia and Divisors | 1033A - King Escape |
39D - Cubical Planet | 1453A - Cancel the Trains |
645A - Amity Assessment | 1144A - Diverse Strings |
1553B - Reverse String | 1073A - Diverse Substring |